<?php
/**
访客通行
 */

namespace App\Http\Controllers\Property;


use App\Common\json;
use App\Common\SmsClient;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class VisitorsController extends Controller{

    //获取
    public function SelectVisitors(Request $request,$type){
        if($request ->isMethod('post')){
            $arr   = [];
            $user_id      = $request ->input('user_id');
            $data         = DB::table('visitors')
                          ->where('user_visitors',$user_id)
                          ->where('visitors_state',$type)
                          ->orderby('visitors_time','ase')
                          ->get();
            foreach ($data as $value){
                $da[]      = array(
                    'visitors_id'  =>$value->visitors_id,
                    'user_visitors' =>$value->user_visitors,
                    'visitors_name'  =>$value->visitors_name,
                    'visitors_sex'   =>$value->visitors_sex,
                    'drive'           =>$value->drive,
                    'license'         =>$value->license,
                    'number'          =>$value->visitors_number,
                    'creation_time'  =>$value->visitors_create_time,
                    'visitors_time'  =>$value->visitors_time

                );
            }
            if($data == true){
                return json::json('success', '获取访问信息成功', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //添加
    public function AddVisitors(Request $request){
        if($request ->isMethod('post')){
            $arr   = (object)array();
            $user_id      = $request ->input('user_id');
           // $village_name = $request ->input('village_name');
            $visitors_name = $request ->input('visitors_name');
            $visitors_sex  = $request ->input('visitors_sex');
            $drive         = $request ->input('drive');
            $license       = $request ->input('license');
            $number        = $request ->input('number');
            $visitors_time = $request ->input('visitors_time');
            $time          = date('Y-m-d H:i:s');
            $toke          = sha1(date('Y-m-d H:i:s').$visitors_name.$visitors_time);
            $data          = DB::table('visitors')
                           ->insertGetId([
                               'user_visitors' => $user_id,
                               'visitors_name' => $visitors_name,
                               'visitors_sex'  => $visitors_sex,
                               'drive'          => $drive,
                               'license'        => $license,
                               'visitors_number'         => $number,
                               'visitors_time' => $visitors_time,
                               'visitors_toke'           =>$toke,
                               'visitors_create_time'    =>$time
                           ]);
            $arrs           = array(
                'visitors_id'   =>$data,
                'user_visitors' => $user_id,
                'visitors_name' => $visitors_name,
                'visitors_sex'  => $visitors_sex,
                'drive'          => $drive,
                'license'        => $license,
                'visitors_number'         => $number,
                'visitors_time' => $visitors_time,
                'visitors_toke'           =>$toke,
                'visitors_create_time'    =>$time);
            if($data == true){
                return json::json('success', '访问信息添加成功', $arrs);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //修改
    public function UpdateVisitors(Request $request){
        if($request ->isMethod('post')){
            $arr          = (object)array();
            $user_id      = $request ->input('visitors_id');
            $visitors_name = $request ->input('visitors_name');
            $visitors_sex  = $request ->input('visitors_sex');
            $drive         = $request ->input('drive');
            $license       = $request ->input('license');
            $number        = $request ->input('number');
            $visitors_time = $request ->input('visitors_time');
            $time          = date('Y-m-d H:i:s');
            $toke          = sha1(date('Y-m-d H:i:s').$visitors_name.$visitors_time);
            $data          = DB::table('visitors')
                ->where('visitors_id',$user_id)
                ->update([
                    'visitors_name' => $visitors_name,
                    'visitors_sex'  => $visitors_sex,
                    'drive'          => $drive,
                    'license'        => $license,
                    'visitors_number'         => $number,
                    'visitors_time' => $visitors_time,
                    'visitors_toke'           =>$toke,
                    'visitors_update_time'   =>$time
                ]);
            if($data == true){
                return json::json('success', '访问信息修改成功', $arr);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //删除
    public function DelVisitors(Request $request){
        if($request ->isMethod('post')){
            $arr                 =(object)array();
            $visitors_id         = $request ->input('visitors_id');
            $data          = DB::table('visitors')
                ->where('visitors_id',$visitors_id)
                ->delete();
            if($data == true){
                return json::json('success', '访问信息删除成功', $arr);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //详情
    public function InfoVisitors(Request $request){
        if($request ->isMethod('post')){
            $arr  = (object)array();
            $visitors_id      = $request ->input('visitors_id');
            $data             = DB::table('visitors')
                              ->leftjoin('user','visitors.user_visitors','=','user.user_id')
                              ->leftjoin('village','user.user_village','=','village.village_id')
                              ->where('visitors_id',$visitors_id)

                              ->get();
            foreach ($data as $value){
                $address     =$value ->user_address.$value->village_name.$value->floor.$value->number.$value->room;
                $da          = array(
                    'visitors_toke'     =>$value->visitors_toke,
                    'nickname'=>$value->nickname,
                    'phone'=>$value->phone,
                    'address'=>$address,
                    'visitors_name'=>$value->visitors_name,
                    'visitors_sex'=>$value->visitors_sex,
                    'drive'=>$value->drive,
                    'license'=>$value->license,
                    'number'=>$value->visitors_number,
                    'time'  =>$value ->visitors_create_time,
                    'visitors_time'=>$value->visitors_time,
                    'visitors_state'=>$value->visitors_state);
            }
            if($data == true){
                return json::json('success', '访问详情获取成功', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }
    }

   /* public function OpinionVisitors(Request $request,$type){
        if($request ->isMethod('post')){
            $user_id      = $request ->input('user_id');
            $data         = DB::table('visitors')
                ->where('user_visitors',$user_id)
                ->get();
            if($data == true){
                return json::json('success', '获取访问信息成功', $data);
            }else{
                return json::json('failure','由于网络或者不正当原因失败','');
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }*/


}


